<?php

class Ext_Tracker_EventMessage extends Ext_Message
{
    const VERSION = '1.0';

    public function __construct( $msg = null )
    {
        $this->getData( $msg );
    }

    public function getData( $msg = null )
    {
        $data = parent::getData( $msg );
        if ( $data != null ) {
            // Validate the message somehow.
            if ( ! isset( $data[ 'version' ] ) || $data[ 'version' ] != Ext_Tracker_EventMessage::VERSION ) {
                throw new Exception( 'Event Message Invalid Version' );
            }
        } else { // Initialize Data.
            $data = array( 
                            'version' => Ext_Tracker_EventMessage::VERSION 
            );
            parent::setData( $data );
        }
        return $data;
    }

    

    public function getEventId()
    {
        $data = $this->getData();
        if ( ! isset( $data[ 'eventId' ] ) ) {
            throw new Exception( 'Tracker external Event eventId not found' );
        }
        return $data[ 'eventId' ];
    }

    public function setEventId( $eventId )
    {
        $data = $this->getData();
        $data[ 'eventId' ] = $eventId;
        parent::setData( $data );
    }

    public function getOrganizationId()
    {
        $data = $this->getData();
        if ( ! isset( $data[ 'organizationId' ] ) ) {
            throw new Exception( 'Tracker external Event organizationId not found' );
        }
        return $data[ 'organizationId' ];
    }

    public function setOrganizationId( $organizationId )
    {
        $data = $this->getData();
        $data[ 'organizationId' ] = $organizationId;
        parent::setData( $data );
    }

    public function getProductId()
    {
        return $this->getDestinationId();
    }

    public function setProductId( $productId )
    {
        return $this->setDestinationId( $productId );
    }

    public function getDestinationId()
    {
        $data = $this->getData();
        if ( ! isset( $data[ 'productId' ] ) ) {
            throw new Exception( 'Tracker external Event productId not found' );
        }
        return $data[ 'productId' ];
    }

    public function setDestinationId( $destinationId )
    {
        $data = $this->getData();
        $data[ 'productId' ] = $destinationId;
        parent::setData( $data );
    }

    // Data saved in the result log.
    public function getExtraData()
    {
        $data = $this->getData();
        if ( isset( $data[ 'extraData' ] ) ) {
            return $data[ 'extraData' ];
        }
        return null;
    }

    public function setExtraData( $extraData )
    {
        $data = $this->getData();
        $data[ 'extraData' ] = $extraData;
        parent::setData( $data );
    }

}